package com.ruoyi.study.common;

import cn.hutool.core.util.RandomUtil;

import java.util.Random;

/**
 * 生成学习卡编号
 * @author chenjuncai
 */
public class GenerateLearningCardNumberUtil {
    public static  char[] letters = {
            '0','1','2','3','4','5','6','7','8','9',
            'A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T',
            'U','V','W','X','Y','Z','a','b','c','d',
            'e','f','g','h','i','j','k','l','m','n',
            'o','p','q','r','s','t','u','v','w','x',
            'y','z'};
    /**
     *１２位随机不重复字母加数字
     * 规则：6位不重复字母+6位为系统时间戳
     * @return 结果
     */
    public static String generateLearningCardNumber(int n){
        return generateUniqueLetters(8)+generateTimestamp(4);
    }

    /**
     * 根据位数生成字母
     * @param n 位数
     * @return 结果 6位不重复字母
     */
    public static String generateUniqueLetters(int n){
        int length = letters.length;
        if(length<n){
            return null;
        }
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i =0 ; i<n;i++){
            int index = random.nextInt(length);
            sb.append(letters[index]);
        }
        return sb.toString();
    }
    /**
     * 根据位数生成6位时间戳
     * @param n 位数
     * @return 结果 6位生成时间戳
     */
    public static String generateTimestamp(int n){
        String timeStamp = String.valueOf(System.currentTimeMillis());
        char[] chars = timeStamp.toCharArray();
        int length = chars.length;
        Random random = new Random();
        if(length<n){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i =0 ; i<n;i++){
            int index = random.nextInt(length);
            sb.append(chars[index]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateLearningCardNumber(6));
    }
}
